package com.hu.video.controller;

import com.hu.video.entity.UserBlockRelations;
import com.hu.video.service.IUserBlockRelationsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Collections;
import com.hu.video.entity.dto.UserBlockDTO;

@RestController
@RequestMapping("/api/block")
public class UserBlockRelationsController {
    @Autowired
    private IUserBlockRelationsService service;

    @GetMapping("/{userId}")
    public List<UserBlockDTO> getBlocks(@PathVariable Long userId) {
        try {
            return service.getUserBlocksWithNames(userId);
        } catch (Exception e) {
            e.printStackTrace();
            return Collections.emptyList();
        }
    }

    @GetMapping("/debug/{userId}")
    public String debugBlocks(@PathVariable Long userId) {
        try {
            List<UserBlockRelations> blocks = service.getByUserId(userId);
            return "Debug: " + blocks.toString();
        } catch (Exception e) {
            return "Error: " + e.getMessage();
        }
    }

    @PostMapping("/add")
    public String addBlock(@RequestBody UserBlockRelations block) {
        try {
            service.addBlock(block);
            return "success";
        } catch (Exception e) {
            return e.getMessage();
        }
    }

    @PostMapping("/remove")
    public String removeBlock(@RequestBody UserBlockRelations block) {
        try {
            service.removeBlock(block);
            return "success";
        } catch (Exception e) {
            return e.getMessage();
        }
    }
} 